package com.tj.collection.service.impl;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.google.common.collect.Lists;
import com.tj.collection.db.dao.BizStatsSqlDao;
import com.tj.collection.db.entity.BizStatsSqlEntity;
import com.tj.collection.service.BizStatsSqlService;
import com.tj.collection.service.dto.StatsParamsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/8/1
 * Time: 下午2:21
 */
@Service
public class BizStatsSqlServiceImpl implements BizStatsSqlService {
    private static final Log LOGGER = LogFactory.getLog(BizStatsSqlServiceImpl.class);

    @Autowired
    private BizStatsSqlDao bizStatsSqlDao;

    @Override
    public StatsParamsVo getStatsParamsByKey(String key) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("deleted", 0);
        paramsMap.put("code", key);
        List<BizStatsSqlEntity> entities = bizStatsSqlDao.selectByParams(paramsMap);
        if (CollectionUtils.isEmpty(entities)) {
            return null;
        }

        if (entities.size() != 1) {
            throw new ApplicationException(key + "查询统计表失败");
        }
        BizStatsSqlEntity entity = entities.get(0);
        StatsParamsVo statsParamsVo = new StatsParamsVo();
        statsParamsVo.setStatsBizCode(entity.getCode());
        statsParamsVo.setReplaceList(StringUtils.isEmpty(entity.getReplaceList()) ?
                null : Lists.newArrayList(entity.getReplaceList().split(",")));
        statsParamsVo.setTemplateSql(entity.getTemplateSql());
        statsParamsVo.setResultSetType(entity.getResultSetType());
        return statsParamsVo;
    }

    @Override
    public Object getData(String queryKey, Map<String, Object> paramsMap) {

        LOGGER.info(" 999991 执行的统计sql,queryKey:{}", JSON.toJSONString(paramsMap));
        StatsParamsVo statsParamsVo = this.getStatsParamsByKey(queryKey);
        if (statsParamsVo == null) {
            return null;
        }

        statsParamsVo.generateSql(paramsMap);
        LOGGER.info("99999 待执行SQL:{}", statsParamsVo.getExecuteSql());
        List<Object> objectList = bizStatsSqlDao.executeStatsSql(statsParamsVo.getExecuteSql());
        if (objectList == null || objectList.size() == 0) {
            if (statsParamsVo.getResultSetType() == 0) {
                return null;
            } else {
                return new ArrayList<>();
            }
        }
        if (statsParamsVo.getResultSetType() == 0) {
            if (objectList.size() != 1) {
                throw new ApplicationException(queryKey + "查询返回结果数不为1");
            }
            return objectList.get(0);
        } else {
            return objectList;
        }
    }
}
